﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Money.Interfaces;
using Money.Data;
using One.Framework;

namespace Money.Services
{
    public class CategoryService : ICategoryService
    {

        Money.Data.MoneyManagerDataContext context = new MoneyManagerDataContext();

        public CategoryService()
        {
            context = new MoneyManagerDataContext();
        }

        public List<Category> GetCategories()
        {
            return context.Categories.ToList<Category>();
        }

        public Category GetCategoryById(int id)
        {
            return context.Categories.SingleOrDefault<Category>(ac => ac.Id == id);
        }

        public List<Category> GetCategories(int parent, bool hasParent)
        {
            try
            {
                if(hasParent)
                    return context.Categories.Where(c => c.ParentId == parent).ToList<Category>();
                else
                    return context.Categories.Where(c => c.ParentId.HasValue == hasParent).ToList<Category>();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void CreateCategory(Category item)
        {
            try
            {
                context.Categories.InsertOnSubmit(item);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot create category");
            }
        }

        public void DeleteCategory(int id)
        {
            try
            {
                Category item = GetCategoryById(id);
                context.Categories.DeleteOnSubmit(item);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot delete category");
            }
        }

        public void UpdateCategory(Category item)
        {
            try
            {
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot update category");
            }
        }

        public List<Category> GetCategories(string orderBy, string orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                var query = from c in context.Categories
                            select c;
                return query.OrderByPaging<Category>(orderBy, orderDirection, page, pageSize, out totalRecords).ToList<Category>();
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new Exception("Cannot get categories");
            }
        }

        public bool CanDelete(int id)
        {
            Category item = GetCategoryById(id);
            if (item != null)
            {
                if (item.Categories.Count > 0 || item.Transactions.Count > 0 || item.MemorizedTransactions.Count > 0)
                    return false;
            }
            return true;
        }
    }
}
